﻿using System;
using Autodesk.Revit.DB;
using YJKCopyModel.CommonTrans;
using YJKCopyModel.LastFastModeling;
using YJKCopyModel.Models;
using YJKCopyModel.QuickModelFrame;
using YJKPresentation.Utils;

namespace YJKCopyModel.FastModelConvert
{
	[Rebuilder(RebuilderName = "FastModeConvert")]
	public class FastModelConvertRebuilder : IRevitModelRebuilder
	{
		public bool TryRebuildRevitModel(Autodesk.Revit.DB.Document inputDoc, RevitModelRequest inputReqeust, out BuildData buildData)
		{
			buildData = new BuildData();
			bool result;
			try
			{
				FastModelConvertRevitModelRequest fastModelConvertRevitModelRequest = inputReqeust as FastModelConvertRevitModelRequest;
				bool flag = fastModelConvertRevitModelRequest == null;
				if (flag)
				{
					result = false;
				}
				else
				{
					FastModelConvertPara fastModelConvertPara = fastModelConvertRevitModelRequest.ViewParas as FastModelConvertPara;
					bool flag2 = fastModelConvertPara == null;
					if (flag2)
					{
						result = false;
					}
					else
					{
						string text = string.Empty;
						bool flag3 = false;
						Transaction transaction = new Transaction(inputDoc, "Replace");
						try
						{
							transaction.Start();
							flag3 = this.ReplaceBlock(inputDoc, fastModelConvertRevitModelRequest, fastModelConvertPara, out buildData);
							bool flag4 = !flag3;
							if (flag4)
							{
								transaction.RollBack();
							}
						}
						catch (Exception ex)
						{
							flag3 = false;
							text = ex.Message;
							transaction.RollBack();
						}
						finally
						{
							bool flag5 = (int)transaction.GetStatus() == 1;
							if (flag5)
							{
								transaction.Commit();
							}
						}
						result = flag3;
					}
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		private bool ReplaceBlock(Autodesk.Revit.DB.Document doc, FastModelConvertRevitModelRequest useRequest, FastModelConvertPara viewParas, out BuildData createdData)
		{
			createdData = new BuildData();
			FamilySymbol placeFs = useRequest.PlaceFs;
			PointPosBlockInfo block = useRequest.Block;
			bool flag = block == null || placeFs == null;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				XYZ xyz = this.GetBlockPlacePoint(block, viewParas.LocationType);
				xyz = this.RemoveZValue(xyz);
				createdData.CreatedElement = RevitCommonFunctions.NewFamilyInstance(doc, placeFs, xyz, doc.ActiveView.GenLevel, (Autodesk.Revit.DB.Structure.StructuralType)0);
				bool flag2 = createdData.CreatedElement == null;
				if (flag2)
				{
					result = false;
				}
				else
				{
					createdData.NeedResponse = true;
					doc.Regenerate();
					XYZ xyz2 = this.GetInstancePlacePoint(createdData.CreatedElement as FamilyInstance, viewParas.LocationType);
					bool flag3 = xyz2 == null;
					if (flag3)
					{
						result = false;
					}
					else
					{
						xyz2 = this.RemoveZValue(xyz2);
						XYZ xyz3 = xyz - xyz2;
						bool flag4 = !xyz3.IsAlmostEqualTo(XYZ.Zero);
						if (flag4)
						{
							BussinessCommon.MoveElement(doc, createdData.CreatedElement, xyz3);
						}
						bool flag5 = false;
						bool flag6 = false;
						BussinessCommon.CheckBlockIsMirrored(block.UseScale, out flag5, out flag6);
						bool flag7 = flag5;
						if (flag7)
						{
							BussinessCommon.MirrorElement(doc, createdData.CreatedElement, xyz, -1);
						}
						bool flag8 = flag6;
						if (flag8)
						{
							BussinessCommon.MirrorElement(doc, createdData.CreatedElement, xyz, 1);
						}
						double num = block.Angle;
						num = RevitCommonFunctions.GetRightAngle(num);
						bool flag9 = this.CheckValue(num) != 0;
						if (flag9)
						{
							BussinessCommon.RotateElement(doc, createdData.CreatedElement, xyz, num);
						}
						Parameter parameter = createdData.CreatedElement.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM);
						bool flag10 = parameter != null;
						if (flag10)
						{
							parameter.Set(Common.MMToFeet(viewParas.Offset * 1000.0));
						}
						result = true;
					}
				}
			}
			return result;
		}

		private XYZ GetBlockPlacePoint(PointPosBlockInfo info, int locationType)
		{
			bool flag = locationType == 0 || locationType == 1;
			XYZ result;
			if (flag)
			{
				XYZ xyz = null;
				bool flag2 = !BussinessCommon.GetBlockCenter(info, out xyz);
				if (flag2)
				{
					result = null;
				}
				else
				{
					result = xyz;
				}
			}
			else
			{
				result = info.UseLocationPoint;
			}
			return result;
		}

		private XYZ GetInstancePlacePoint(FamilyInstance fi, int locationType)
		{
			bool flag = fi == null;
			XYZ result;
			if (flag)
			{
				result = null;
			}
			else
			{
				bool flag2 = locationType == 0;
				if (flag2)
				{
					XYZ elementBoxCenter = RevitCommonFunctions.GetElementBoxCenter(fi.Document, fi);
					result = elementBoxCenter;
				}
				else
				{
					result = RevitCommonFunctions.GetElementLocation(fi);
				}
			}
			return result;
		}

		private XYZ RemoveZValue(XYZ point)
		{
			return new XYZ(point.X, point.Y, 0.0);
		}

		private int CheckValue(double value)
		{
			ToleranceManger toleranceManger = ToleranceManger.GetToleranceManger();
			double tolerance = toleranceManger.GetTolerance("末端快模-数字公差");
			bool flag = Geometry.IsEqual(value, 0.0, tolerance);
			int result;
			if (flag)
			{
				result = 0;
			}
			else
			{
				bool flag2 = value > 0.0;
				if (flag2)
				{
					result = 1;
				}
				else
				{
					result = -1;
				}
			}
			return result;
		}
	}
}
